package quartz;

import org.apache.commons.lang3.time.DateUtils;
import org.quartz.JobDetail;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.Trigger;
import org.quartz.impl.StdSchedulerFactory;
import quartz.job.MyJob3;
import quartz.job.MyJob4;

import java.text.ParseException;
import java.util.Date;
import java.util.concurrent.TimeUnit;

import static org.quartz.JobBuilder.newJob;
import static org.quartz.SimpleScheduleBuilder.simpleSchedule;
import static org.quartz.TriggerBuilder.newTrigger;

public class Test4 {
    public static void main(String[] args) throws Exception {
        test1();
    }

    /**
     * 和test2一样,job1秒没执行完,但是每秒都会触发,但是@DisallowConcurrentExecution了,所以会排队,等可以执行了立刻执行.
     *
     * @throws SchedulerException
     */
    public static void test1() throws Exception {

        StdSchedulerFactory schedFact = new StdSchedulerFactory();

        Scheduler sched = schedFact.getScheduler();

        sched.start();

        // define the job and tie it to our HelloJob class

        JobDetail job = newJob(MyJob4.class)
                .withIdentity("myJob", "group1")
                .build();


        // Trigger the job to run now, and then every 40 seconds
        Trigger trigger = newTrigger()
                .withIdentity("myTrigger", "group2")
                .startNow()
                .withSchedule(simpleSchedule()
                        .withIntervalInSeconds(1)
                        .withRepeatCount(5)
                )
                .startAt(DateUtils.parseDate("2018-01-01", "yyyy-MM-dd"))
                .build();


        // Tell quartz to schedule the job using our trigger
        try {
            Thread.sleep(TimeUnit.SECONDS.toMillis(2));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("启动时间" + new Date());
        sched.scheduleJob(job, trigger);
        System.out.println("结束时间" + new Date());
        System.out.println(trigger.getStartTime());
        System.out.println(trigger.getEndTime());
    }

}



